package com.controller.admin;

import com.domain.Blog;
import com.domain.PageBean;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.BlogService;
import com.util.BlogIndex;
import com.util.DateJsonValueProcessor;
import com.util.ResponseUtil;
import com.util.StringUtil;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 98272
 */
@Log4j2
@Controller
@RequestMapping("/admin/blog")
public class BlogAdminController {
    @Resource
    private BlogService blogService;
    BlogIndex blogIndex = new BlogIndex();

    @RequestMapping("/list.do")
    public String list(@RequestParam(value = "page", required = false,defaultValue = "0") String page, @RequestParam(value = "rows", required = false,defaultValue = "20") String rows, Blog blog, HttpServletResponse response) throws IOException, InvocationTargetException, IllegalAccessException {
        Map<String, Object> map = new HashMap<>();
        map.put("title", StringUtil.formatLike(blog.getTitle()));
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(rows));
        List<Blog> blogList=blogService.findAll(map);
        PageInfo pi=new PageInfo(blogList);
        Long total = blogService.getTotal(map);

        JSONObject result = new JSONObject();
        JsonConfig config = new JsonConfig();
        config.registerJsonValueProcessor(Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray = JSONArray.fromObject(pi.getList(), config);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 保存博客
     */
    @RequestMapping("/save.do")
    public String save(Blog blog, HttpServletResponse response) throws IOException {
        Integer total = 0;
        if (blog.getId() != null) {
            total = blogService.update(blog);
            blogIndex.updateIndex(blog);
        } else {
            total = blogService.add(blog);
            blogIndex.addIndex(blog);
        }
        JSONObject res = new JSONObject();
        if (total > 0) {
            res.put("success", Boolean.valueOf(true));
        } else {
            res.put("success", Boolean.valueOf(false));
        }
        ResponseUtil.write(response, res);
        return null;
    }

    @RequestMapping("/delete.do")
    public String delete(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        String[] idsStr = ids.split(",");
        JSONObject result = new JSONObject();
        for (int i = 0; i < idsStr.length; i++) {
            blogService.delete(Integer.parseInt(idsStr[i]));
            blogIndex.deleteIndex(idsStr[i]);
        }
        result.put("success", Boolean.valueOf(true));
        ResponseUtil.write(response, result);
        return null;
    }

    @RequestMapping("/findById.do")
    public String findById(@RequestParam("id") String id, HttpServletResponse response) throws IOException {
        Blog blog = blogService.findById(Integer.parseInt(id));
        JSONObject jsonObject = JSONObject.fromObject(blog);
        ResponseUtil.write(response, jsonObject);
        return null;
    }
}
